<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class WechatUser extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'wechat_users';

    protected $fillable = [
        'openid',
        'session_key',
        'unionid',
        'nickname',
        'avatar',
        'gender',
        'country',
        'province',
        'city',
        'language',
        'phone',
        'email',
        'subscribe_time',
        'unsubscribe_time',
        'subscribe_status',
        'remark',
        'privileges',
    ];

    protected $casts = [
        'privileges' => 'array',
        'subscribe_time' => 'datetime',
        'unsubscribe_time' => 'datetime',
        'gender' => 'integer',
        'subscribe_status' => 'integer',
    ];

    protected $dates = [
        'subscribe_time',
        'unsubscribe_time',
        'deleted_at',
    ];

    /**
     * 性别常量
     */
    const GENDER_UNKNOWN = 0;
    const GENDER_MALE = 1;
    const GENDER_FEMALE = 2;

    /**
     * 关注状态常量
     */
    const SUBSCRIBE_STATUS_UNSUBSCRIBED = 0;
    const SUBSCRIBE_STATUS_SUBSCRIBED = 1;

    /**
     * 获取性别文本
     *
     * @return string
     */
    public function getGenderTextAttribute()
    {
        $genderMap = [
            self::GENDER_UNKNOWN => '未知',
            self::GENDER_MALE => '男',
            self::GENDER_FEMALE => '女',
        ];

        return $genderMap[$this->gender] ?? '未知';
    }

    /**
     * 获取关注状态文本
     *
     * @return string
     */
    public function getSubscribeStatusTextAttribute()
    {
        return $this->subscribe_status ? '已关注' : '未关注';
    }

    /**
     * 获取完整地址
     *
     * @return string
     */
    public function getFullAddressAttribute()
    {
        $address = [];
        if ($this->country) $address[] = $this->country;
        if ($this->province) $address[] = $this->province;
        if ($this->city) $address[] = $this->city;
        
        return implode(' ', $address);
    }

    /**
     * 根据openid查找用户
     *
     * @param string $openid
     * @return WechatUser|null
     */
    public static function findByOpenid($openid)
    {
        return static::where('openid', $openid)->first();
    }

    /**
     * 根据unionid查找用户
     *
     * @param string $unionid
     * @return WechatUser|null
     */
    public static function findByUnionid($unionid)
    {
        return static::where('unionid', $unionid)->first();
    }

    /**
     * 获取已关注用户
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeSubscribed($query)
    {
        return $query->where('subscribe_status', self::SUBSCRIBE_STATUS_SUBSCRIBED);
    }

    /**
     * 获取未关注用户
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeUnsubscribed($query)
    {
        return $query->where('subscribe_status', self::SUBSCRIBE_STATUS_UNSUBSCRIBED);
    }
}